#include "CommModule.h"
#include <thread>
#include <vector>
#include <chrono>
#include <iostream>
#include "Logger.h"
#include "TcpModule.h"
#include "timer.hpp"
#include "CanModule.h"
#include"crc_calc.h"
#include "MutuleCanModule.h"
#include "Uart.h"
void callback1(void* arg)
{
Buffer* buffer=static_cast<Buffer*>(arg);
     int64_t value=0;
     int len=buffer->read((uint8_t*)&value,sizeof(value));
     printf("callback1 %ld\n",value);
}
void callback2(void* arg)
{
    Buffer* buffer=static_cast<Buffer*>(arg);
     int64_t value=0;
     int len=buffer->read((uint8_t*)&value,sizeof(value));
     printf("callback2 %ld\n",value);
}
void test_common()
{
    CommModule c1("hello");
    c1.open_read();
    c1.set_read_callback(callback1);
    CommModule c2("hello2");
    c2.open_read();
    c2.set_read_callback(callback2);
    while (1)
    {
        c1.write_message("hello2", 12);
        c2.write_message("hello", 13);

        sleep(1);
    }
}
void recv_tcp_msg(void* arg)
{
    Buffer* buffer=static_cast<Buffer*>(arg);
     char buf[1024]={0};
     int len=buffer->read((uint8_t*)buf,1024);
     printf("%s\n",buf);
}
void test_tcp()
{
    TcpModule server("1", 8080);
    server.set_conn_read_back(recv_tcp_msg);
    while (1)
    {
        sleep(1);
    }
    
}
#include "timer.hpp"

// 定时任务回调函数
void timer_task(void* arg) {
    std::cout << "Timer task 1 triggered at " << Timer::get_instance().get_current_time() << " ms" << std::endl;
}

void timer_task2(void* arg) {
    std::cout << "Timer task 2 triggered at " << Timer::get_instance().get_current_time() << " ms" << std::endl;
}
void test_timer()
{
    Timer &timer = Timer::get_instance(); // 获取 Timer 单例

    // 绑定信号处理函数
    signal(SIGUSR1, handle_signal);

    // 启动定时器
    timer.start();

    // 添加定时任务
    TimerTask task1 = {500, timer_task, nullptr};
    timer.add_task(task1);

    TimerTask task2 = {1000, timer_task2, nullptr};
    timer.add_task(task2);

    // 模拟主线程的其他工作
    std::this_thread::sleep_for(std::chrono::seconds(10));

    // 停止定时器
    timer.stop();
}
void test_mult_can()
{
   MutuleCanModule module("can0");
    MutuleCanModule module1("can0");
    module.open_read();
    uint32_t frame_id = 0;  // Unique identifier for each frame
    uint8_t full_data[1024];
    while (1)
    {
        // Prepare the frame data with the sequence number
        frame_id++;
        snprintf((char*)full_data, sizeof(full_data), "ID: %u, Data: %s", frame_id);
        printf("send %s\n",full_data);
        // Send the frame
        module1.mutule_write_data(0, full_data, strlen((char*)full_data));
        
        // Optionally, print to verify
        printf("Sent Frame ID: %u\n", frame_id);
        
        // Small delay to prevent overwhelming the receiver
        // usleep(50000);  // Delay in microseconds (adjust as needed)
        memset(full_data,0,sizeof(full_data));
        sleep(1);
        // msleep(500);
        // usleep(100000);
    }
}
void recv_can_msg(void* arg)
{
    printf("have can msg\n");
}
void test_can()
{
    CanModule can("can0");
    can.open_read();
    can.set_read_callback(recv_can_msg);
    while (1)
    {
        sleep(1);
    }
    
}
void recv_uart_msg(void* arg)
{
     Buffer* m_read_buffer=static_cast<Buffer*>(arg);
     char buf[1024]={0};
     int len=m_read_buffer->read((uint8_t*)buf,1024);
     printf("%s\n",buf);
}

void test_uart()
{
     std::string device = "/dev/ttyRS485-1";
    Uart uart(device,115200);
    uart.open_read();
    uart.set_read_callback(recv_uart_msg);
    const char *data = "Hello UART!";
    
    while (1)
    {
         // 发送数据
    // uart.writeData(data, strlen(data));
// readData
 // 接收数据
    // 
    sleep(1);
    }

}
int main(int argc, const char **argv)
{
    pthread_setname_np(pthread_self(), "main");
    // test_can();
    // test_mult_can();
    // test_uart();
    // test_tcp();
    // test_timer();
   test_common();
    

    return 0;
}